# dataParser.py
# Lisa Soros
# CAP 5512
# Parses the GA output data to extract relevant data and perform 
# statistical analyses

import numpy
import math
outfile = open('generational_avgs.txt', 'w')
genAvgList = []
lineList = []
bestAvgList = []
ciUpperList = []
ciLowerList = []
aciUpperList = []
aciLowerList = []
avgStdList = []

avgAvgList2 = []
avgAvgList3 = []
avgAvgList4 = []
avgAvgList5 = []
avgAvgList6 = []
avgAvgList7 = []
avgAvgList8 = []
avgAvgList9 = []
avgAvgList10 = []


#####################################################
# Calculating averages and standard deviations for  #
# best & average fitness for individual generations #
#####################################################
# k=2
for i in range(100):
	infile = open('KM.2.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[4]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList2.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()
	
#k=3
for i in range(100):
	infile = open('KM.3.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList3.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()	

#k=4	
for i in range(100):
	infile = open('KM.4.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList4.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()
	
#k=5
for i in range(100):
	infile = open('KM.5.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList5.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()	
	
#k=6
for i in range(100):
	infile = open('KM.6.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList6.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()	

#k=7	
for i in range(100):
	infile = open('KM.7.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList7.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()
		
#k=8
for i in range(100):
	infile = open('KM.8.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList8.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()
	
#k=9
for i in range(100):
	infile = open('KM.9.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList9.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()

#k=10
for i in range(100):
	infile = open('KM.10.20_summary.txt')
	print(str(i))
	for line in infile.readlines():
		if line.startswith(' R '):
			lineList = line.split()
			if  int(lineList[3]) == i:
				genAvgList.append(float(lineList[5]))
	avgAvg = str(numpy.average(genAvgList))
	avgAvgList10.append(avgAvg)
	avgStd = str(numpy.std(genAvgList))
	aciUpperList.append(numpy.average(genAvgList) + numpy.std(genAvgList))
	aciLowerList.append(numpy.average(genAvgList) - numpy.std(genAvgList))
	outList = [str(i), avgAvg, avgStd]
	outfile.write(str(outList) + '\n')
	del lineList[:]
	del genAvgList[:]	
	infile.close()	
	
import matplotlib.pyplot as plt
xaxis = numpy.array(range(100))
yaxis2 = numpy.array(avgAvgList2)
yaxis3 = numpy.array(avgAvgList3)
yaxis4 = numpy.array(avgAvgList4)
yaxis5 = numpy.array(avgAvgList5)
yaxis6 = numpy.array(avgAvgList6)
yaxis7 = numpy.array(avgAvgList7)
yaxis8 = numpy.array(avgAvgList8)
yaxis9 = numpy.array(avgAvgList9)
yaxis10 = numpy.array(avgAvgList10)
plt.plot(xaxis, yaxis2, '0.9', xaxis, yaxis3, '0.8', xaxis, yaxis4, '0.7', xaxis, yaxis5, '0.6', xaxis, yaxis6, '0.5', xaxis, yaxis7, '0.4', xaxis, yaxis8, '0.3', xaxis, yaxis9, '0.2', xaxis, yaxis10, '0.1')
plt.xlabel('Generation')
plt.ylabel('Average Best Fitness ')
plt.show()


infile.close()
